import React, { Component } from "react";

import { connect } from "react-redux";
import MainLayout from "@component/mainLayout/mainLayout";
import XFButton from "@component/button/index";
import { Toast } from "antd-mobile";
import { InterfaceG } from "@config/Config";
import "./index.scss";
import sumitSuccess from "@images/submitSuccess.png";
import yhTools from "@tools/yh";

export class xhfLivenessRet extends Component {
	constructor(props) {
		super(props);
		this.state = {
			timeText: `倒计时10秒`,
			time: 10
		};
		this.timer = null;
	}

	componentDidMount() {
		this.countDown();
	}
	componentWillUnmount() {
		clearInterval(this.timer);
	}

	//倒计时
	countDown() {
		const _this = this;
		let { time } = this.state;
		this.timer = setInterval(() => {
			if (time < 1) {
				//倒计时十秒后调授信接口，授信通过跳到首页，未通过调资料提交页面
				clearInterval(_this.timer);
				_this.props.history.push({
					pathname: "/xhfOpenSuccess",
					state: { fromUrl: "xhfHomeCrediting" }
				});
			} else {
				time--;
				_this.setState({
					time,
					timeText: `倒计时${time}秒`
				});
				_this.xhfAppSubApply();
			}
		}, 1000);
	}

	//获取授信接口
	xhfAppSubApply() {
		let objs = {
			url: InterfaceG.queryCreditResult,
			params: {
				lnid: this.props.getAppPlatformsDatas.lnid
			}
		};
		let _that = this;
		yhTools.request(objs).then(res => {
			if (res.code === "0") {
				const status = res.data.creditStatus;
				if (status == "1") {
					clearInterval(_that.timer);
					_that.props.history.push({
						pathname: "/xhfFacilitySuccess"
					});
				} else if (status == "4") {
					clearInterval(_that.timer);
					_that.props.history.push({
						pathname: "/xhfOpenFail",
						state: { message: res.message }
					});
				} else if (status == "2") {
					// clearInterval(_this.timer);
					//   _that.xhfAppSubApply();
				}
			} else {
				Toast.info(res.message, 2);
			}
		});
	}
	btnFn() {}

	render() {
		const { timeText } = this.state;
		let _pageParams = {
			header: {
				title: "开通结果",
				goBackPath: "/xhfHomeCrediting"
			},
			history: this.props.history,
			cacheLifecycles: this.props.cacheLifecycles
		};
		// const btnStyle = {
		// 	height: "0.80rem",
		// 	width: "6.96rem",
		// 	borderRadius: ".40rem"
		// };
		return (
			<MainLayout pageParams={_pageParams}>
				<div className="xhf-liveness-ret">
					<div className="liveness-ret-box">
						<img src={sumitSuccess} />
						<h3>资料处理中</h3>
					</div>
					<XFButton onClick={this.btnFn.bind(this)}>{timeText}</XFButton>
				</div>
			</MainLayout>
		);
	}
}

function mapStateToProps(state) {
	return state;
}

function mapDispatchToProps() {
	return {};
}

const xhfLivenessRetPage = connect(
	mapStateToProps,
	mapDispatchToProps
)(xhfLivenessRet);
export default xhfLivenessRetPage;
